package com.gtw.modules.api.controller;

import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.utils.R;
import com.gtw.dto.TbSpikeDto;
import com.gtw.dto.TbSpikeDto2;
import com.gtw.modules.api.service.TbActivityServices;
import com.gtw.modules.api.service.TbCommodityService;
import com.gtw.modules.entity.TbActivity;
import com.gtw.modules.entity.TbActivityCommodity;
import com.gtw.modules.entity.TbCommodity;
import com.gtw.modules.sys.service.SysTbActivityCommodityService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/** 限时秒抢
 * Created by wuye on 2018/3/21.
 */
@RestController
@RequestMapping("/api/spike")
public class TbSpikeController {

    @Autowired
    private TbActivityServices tbActivityServices;

    @Autowired
    private SysTbActivityCommodityService sysTbActivityCommodityService;

    @Autowired
    private TbCommodityService tbCommodityService;

    /**
     * 查询当天的抢购活动
     * @return
     */
    @AuthIgnoreApi
    @GetMapping("/foundBySpikeList")
    public R foundBySpikeList(){
        //查询当天所有抢购活动
        List<TbActivity> activityList = tbActivityServices.foundBySpike();
        //查询每个抢购活动对应的商品
        List<TbSpikeDto> dtoList = new ArrayList<>();
        for (TbActivity activity:activityList) {
            TbSpikeDto dto = new TbSpikeDto();
            List<TbActivityCommodity> activityCommodityList = sysTbActivityCommodityService.foundByActivityId(activity.getId());
            //每个活动对应的商品列表
            List<TbCommodity> commodityList = new ArrayList<>();
            for (TbActivityCommodity tac: activityCommodityList) {
                TbCommodity tbCommodity = tbCommodityService.queryObject(tac.getCommodiytid());
                commodityList.add(tbCommodity);
            }
            dto.setCommodityList(commodityList);
            dto.setActivityCommodityList(activityCommodityList);
            dto.setTbActivity(activity);
            dtoList.add(dto);
        }
        return R.ok().put("data",dtoList);
    }

    /**
     * 根据抢购ID和商品ID查询抢购活动指定商品信息
     * @return
     */
    @AuthIgnoreApi
    @GetMapping("/fundBySpikeId")
    public R fundBySpikeId(@RequestParam Integer activityid,@RequestParam Integer commondityid){
        Map<String,Object> map = new HashedMap();
        map.put("activityid",activityid);
        map.put("commondityid",commondityid);
        TbActivity tbActivity = tbActivityServices.selectByPrimaryKey(activityid);
        TbActivityCommodity tbActivityCommodity =sysTbActivityCommodityService.foundByActivityidCommodityid(map);
        TbSpikeDto2 dto = new TbSpikeDto2();
        dto.setTbActivity(tbActivity);
        TbCommodity tbCommodity = tbCommodityService.queryObject(tbActivityCommodity.getCommodiytid());
        dto.setTbCommodity(tbCommodity);
        dto.setTbActivityCommodity(tbActivityCommodity);
        return R.ok().put("data",tbActivityCommodity);
    }
}
